﻿using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Wox.Plugin.Everything.Everything
{
    public enum StateCode
    {
        OK,
        MemoryError,
        IPCError,
        RegisterClassExError,
        CreateWindowError,
        CreateThreadError,
        InvalidIndexError,
        InvalidCallError
    }

    public enum RequestFlag
    {

        FileName = 0x00000001,
        Path = 0x00000002,
        FullPathAndFileName = 0x00000004,
        Extenssion = 0x00000008,
        Size = 0x00000010,
        DateCreated = 0x00000020,
        DateModified = 0x00000040,
        DateAccessed = 0x00000080,
        Attributes = 0x00000100,
        FileListFileName = 0x00000200,
        RunCount = 0x00000400,
        DateRun = 0x00000800,
        DateRecentlyChanged = 0x00001000,
        HighlightedFileName = 0x00002000,
        HighlightedPath = 0x00004000,
        HighlightedFullPathAndFileName = 0x00008000,
    }


    public sealed class EverythingApiDllImport
    {
        public const string DLL = "Everything.dll";

        [DllImport(DLL, CharSet = CharSet.Unicode)]
        internal static extern int Everything_SetSearchW(string lpSearchString);

        [DllImport(DLL)]
        internal static extern void Everything_SetMatchPath(bool bEnable);

        [DllImport(DLL)]
        internal static extern void Everything_SetMatchCase(bool bEnable);

        [DllImport(DLL)]
        internal static extern void Everything_SetMatchWholeWord(bool bEnable);

        [DllImport(DLL)]
        internal static extern void Everything_SetRegex(bool bEnable);

        [DllImport(DLL)]
        internal static extern void Everything_SetMax(int dwMax);

        [DllImport(DLL)]
        internal static extern void Everything_SetOffset(int dwOffset);

        [DllImport(DLL)]
        internal static extern bool Everything_GetMatchPath();

        [DllImport(DLL)]
        internal static extern bool Everything_GetMatchCase();

        [DllImport(DLL)]
        internal static extern bool Everything_GetMatchWholeWord();

        [DllImport(DLL)]
        internal static extern bool Everything_GetRegex();

        [DllImport(DLL)]
        internal static extern uint Everything_GetMax();

        [DllImport(DLL)]
        internal static extern uint Everything_GetOffset();

        [DllImport(DLL, CharSet = CharSet.Unicode)]
        internal static extern string Everything_GetSearchW();

        [DllImport(DLL)]
        internal static extern StateCode Everything_GetLastError();

        [DllImport(DLL, CharSet = CharSet.Unicode)]
        internal static extern bool Everything_QueryW(bool bWait);

        [DllImport(DLL)]
        internal static extern void Everything_SortResultsByPath();

        [DllImport(DLL)]
        internal static extern int Everything_GetNumFileResults();

        [DllImport(DLL)]
        internal static extern int Everything_GetNumFolderResults();

        [DllImport(DLL)]
        internal static extern int Everything_GetNumResults();

        [DllImport(DLL)]
        internal static extern int Everything_GetTotFileResults();

        [DllImport(DLL)]
        internal static extern int Everything_GetTotFolderResults();

        [DllImport(DLL)]
        internal static extern int Everything_GetTotResults();

        [DllImport(DLL)]
        internal static extern bool Everything_IsVolumeResult(int nIndex);

        [DllImport(DLL)]
        internal static extern bool Everything_IsFolderResult(int nIndex);

        [DllImport(DLL)]
        internal static extern bool Everything_IsFileResult(int nIndex);

        [DllImport(DLL, CharSet = CharSet.Unicode)]
        internal static extern void Everything_GetResultFullPathNameW(int nIndex, StringBuilder lpString, int nMaxCount);
        // https://www.voidtools.com/forum/viewtopic.php?t=8169
        [DllImport(DLL, CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultFileNameW(int nIndex);
        [DllImport(DLL, CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultHighlightedPathW(int nIndex);
        [DllImport(DLL, CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultHighlightedFileNameW(int nIndex);
        [DllImport(DLL, CharSet = CharSet.Unicode)]
        public static extern IntPtr Everything_GetResultHighlightedFullPathAndFileNameW(int nIndex);
        [DllImport(DLL)]
        public static extern int Everything_GetMajorVersion();
        [DllImport(DLL)]
        public static extern int Everything_GetMinorVersion();
        [DllImport(DLL)]
        public static extern int Everything_GetRevision();
        [DllImport(DLL)]
        public static extern void Everything_SetRequestFlags(RequestFlag flag);

        [DllImport(DLL)]
        internal static extern void Everything_Reset();
    }
}